let express = require('express')
let router = express.Router()

let mysqlHelper = require('../utils/mysqlHelper.js')
const utils = require('../utils/utils.js')


// 查询 maoyuan  keyword检索
router.get('/list', async function (req, res, next) {
    let result = {}
    try {
        let page = utils.getQueryValue(req.query.page)
        let pageSize = utils.getQueryValue(req.query.pageSize)
        let sqlLimit = " limit " + (page - 1) * pageSize + ", " + pageSize
        sqlLimit = ((sqlLimit.indexOf('NaN')>-1 || sqlLimit.indexOf("undefined")>-1 || pageSize=='' || page=='') ? "" : sqlLimit)
        let keyword = utils.getQueryValue(req.query.keyword)
        let userId = utils.getQueryValue(req.query.userId)
        let status = utils.getQueryValue(req.query.status)
        let startTime = utils.getQueryValue(req.query.startTime)
        let endTime = utils.getQueryValue(req.query.endTime)
        let sqlWhere = " WHERE 1=1"
        sqlWhere += (keyword!='' ? " and (content like '%" + keyword + "%' OR title like '%"+ keyword+"%')" : "")
        sqlWhere += (userId!='' ? " and a.re_user="+userId+"" : "")
        sqlWhere += (startTime != '' ? " and time>='"+startTime+"'" : "")
        sqlWhere += (endTime != '' ? " and time<='"+endTime+"'" : "")

        total = (await mysqlHelper.query("select count(1) as ct from sys_message a " + sqlWhere))[0].ct
        let prop = utils.getQueryValue(req.query.prop)
        let order = utils.getQueryValue(req.query.order)
        order = (order=='ascending'? 'ASC': 'DESC')
        let sqlOrder = " ORDER by " + prop+" "+ order
        sqlOrder = ((prop.indexOf('NaN')>-1 || order.indexOf("undefined")>-1 || prop=='' || order=='') ? "" : sqlOrder)
        let sqlStr = "select *,u.name as sendName,u2.name as recName FROM sys_message a           \
                        left join sys_user u on a.send_user=u.userId    \
                        left join sys_user u2 on a.re_user=u2.userId "
        sqlStr += sqlWhere + sqlOrder + sqlLimit 
        rows = await mysqlHelper.query(sqlStr)

        result.code = 200
        result.data = {
            'total': total,
            'page': page,
            'pageSize': pageSize,
            'summary': {
                'id': page * pageSize,
                'name': '999'
            },
            rows: rows
        }
        result.message = ''
        result.success = true
    } catch (err) {
        result.code = 400
        result.success = false
        result.message = err.message
    }
    res.json(result)
})

// 预警报警处置
router.post('/track', async function (req, res, next) {
    let result = {}
    try {
        let sqlStr = ""
        let id = utils.getQueryValue(req.body.trackID)
        let alertID = utils.getQueryValue(req.body.alertID)
        let transactionType = utils.getQueryValue(req.body.transactionType)
        let workflowID = utils.getQueryValue(req.body.workflowID)
        let tracker = utils.getQueryValue(req.body.tracker)
        let tracktime = utils.getQueryValue(req.body.tracktime)
        let trackcontent = utils.getQueryValue(req.body.trackcontent)
        if (id == '') { //新增
            sqlStr = "insert into scheme_alert_track(alertID,transactionType,workflowID,tracker,tracktime,trackcontent) values (" + alertID + "," + transactionType + ",'" + workflowID + "'," + tracker+ ",'" + tracktime + "','" + trackcontent+"')"
            await mysqlHelper.query(sqlStr)
        } else { //修改
            let deptStr = Array.isArray(req.body.dept) ? req.body.dept[req.body.dept.length - 1] : req.body.dept
            //let sexStr = utils.getQueryValue(req.body.sex) == '' ? "" : ",sex=" + req.body.sex
            sqlStr = "UPDATE scheme_alert_track SET transactionType=" + transactionType + ",workflowID='"+workflowID+"',tracker=" +tracker + ",tracktime='" + tracktime + "',trackcontent='" + trackcontent + "' WHERE trackID=" + id
            await mysqlHelper.query(sqlStr)
        }
        result.code = 200
        result.success = true
    } catch (err) {
        result.code = 400
        result.success = false
        result.message = err.message
    }
    res.json(result)  
})

module.exports = router

